package com.mairuide.one_click_search.service;

import com.mairuide.one_click_search.dao.DataTemplateFieldDao;
import com.mairuide.one_click_search.dto.DataTemplateDto;
import com.mairuide.one_click_search.pojo.DataTemplateField;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-04-23 16:14
 */
@Service
@Transactional
public class DataTemplateFieldService {
    @Autowired
    DataTemplateFieldDao dataTemplateFieldDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * @Author 刘中华
     * @Date 2019/4/26 14:55
     * @Param [tableName]
     * @return java.util.List<com.mairuide.one_click_search.pojo.DataTemplateField>
     * @Description  根据表名得到字段表数据
     **/
    public List<DataTemplateField> findDataTemplateFieldListByTableName(String tableName){
        String sql="select * from TB_DATA_TEMPLATE_FIELD where is_delete=0 and tablename = '" + tableName + "' order by ORDER_INDEX asc ";
        List<DataTemplateField> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<DataTemplateField>(DataTemplateField.class));
        return  list;
    }
    public Map<String,String> findDataTemplateFieldMapByTableName(String tableName){
        String sql="select * from TB_DATA_TEMPLATE_FIELD where is_delete=0 and tablename = '" + tableName + "' order by ORDER_INDEX asc ";
        List<DataTemplateField> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<DataTemplateField>(DataTemplateField.class));
        Map<String, String> map = new LinkedHashMap<>();
        for (int i = 0; i < list.size(); i++) {
            DataTemplateField field = list.get(i);
            String filedname = field.getFiledname();
            map.put(field.getColname().toLowerCase(), filedname);
        }
       /* map.put("vid","vid");
        map.put("createTime","createTime");
        map.put("isDelete","isDelete");
        map.put("createId","createId");
        map.put("tablename","tablename");*/
        return  map;
    }
    /**
     * @Author 刘中华
     * @Date 2019/4/28 15:43
     * @Param [tableName]
     * @return void
     * @Description 新增模板字段
     **/
    public void addTemplateField(DataTemplateDto dataTemplateDto){
        String tablename = dataTemplateDto.getTablename();
        List<DataTemplateField> list = dataTemplateDto.getDataTemplateFieldList();
        List<Integer> collect = list.stream().filter(m->m.getVid()!=null).map(m -> m.getVid()).collect(Collectors.toList());
        String join = StringUtils.join(collect.toArray(), ",");
        if (StringUtils.isNotBlank(join)){
            String sql="delete from TB_DATA_TEMPLATE_FIELD where tablename='"+dataTemplateDto.getTablename()+"' and vid not in ("+join+")";
            jdbcTemplate.execute(sql);
        }else{
            String sql="delete from TB_DATA_TEMPLATE_FIELD where tablename='"+dataTemplateDto.getTablename()+"'";
            jdbcTemplate.execute(sql);
        }
        ArrayList<String> add = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DataTemplateField dataTemplateField = list.get(i);
            dataTemplateField.setTablename(tablename);
            if (dataTemplateField.getVid()==null){
                if (dataTemplateField.getColwidth()==null){
                    if (dataTemplateField.getSjlx()==2){
                        dataTemplateField.setColwidth(100);
                    }else if (dataTemplateField.getSjlx()==3){
                        dataTemplateField.setColwidth(120);
                    }else if (dataTemplateField.getSjlx()==4){
                        dataTemplateField.setColwidth(170);
                    }else{
                        dataTemplateField.setColwidth(100);
                    }
                }
                String field = getField(tablename,add);
                dataTemplateField.setColname(field);
                dataTemplateField.setCreateTime(new Timestamp(System.currentTimeMillis()));
                dataTemplateField.setIsDelete(0);
                dataTemplateFieldDao.save(dataTemplateField);
                add.add(field);
            }else{
                DataTemplateField one = dataTemplateFieldDao.findOne(dataTemplateField.getVid());
                BeanUtils.copyProperties(dataTemplateField,one,"isDelete","createTime");
                dataTemplateFieldDao.save(one);
            }
        }
    }

    public void delFieldByTableName(String tableName){
        String sql= "update TB_DATA_TEMPLATE_FIELD set IS_DELETE = 1 where TABLENAME= '"+tableName+"'";
        jdbcTemplate.execute(sql);
    }

    public static ArrayList<String> fieldName = new ArrayList<String>(Arrays.asList("F01","F02","F03","F04","F05","F06","F07","F08","F09","F10",
            "F11","F12","F13","F14","F15","F16","F17","F18","F19","F20",
            "F21","F22","F23","F24","F25","F26","F27","F28","F29","F30",
            "F31","F32","F33","F34","F35","F36","F37","F38","F39","F40"));
    /**
     * @Author 刘中华
     * @Date 2019/4/29 9:51
     * @Param []
     * @return java.lang.String
     * @Description  获取字段 f01-f40
      **/
    public String getField(String tablename,List<String> add){
        String sql="select colname from TB_DATA_TEMPLATE_FIELD where  is_delete=0 and  tablename='"+tablename+"'";
        List<String> list = jdbcTemplate.queryForList(sql, String.class);
        List<String> collect = fieldName.stream().filter(m -> !list.contains(m)&&!add.contains(m)).collect(Collectors.toList());
        return collect.size()>0?collect.get(0):null;
    }
    /**
     * @Author 刘中华
     * @Date 2019/5/6 11:05
     * @Param [tableName]
     * @return void
     * @Description  根据模板名字来找到对应的字段映射
     **/
    public Map<String, DataTemplateField>  getColToFieldMap(String tableName){
        String sql="select * from TB_DATA_TEMPLATE_FIELD where is_delete=0 and tablename='"+tableName+"' order by ORDER_INDEX asc ";
        List<DataTemplateField> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DataTemplateField.class));
        Map<String, DataTemplateField> fieldMap = new LinkedHashMap<>();
        for (int i = 0; i < list.size(); i++) {
            DataTemplateField field = list.get(i);
            fieldMap.put(field.getColname(),field);
        }
        return fieldMap;
    }
}
